<template>
  <div class="article-container">
      <!-- 导航栏 -->
    <van-nav-bar
      class="app-nav-bar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
  <div class="main-wrap">
      <!-- 加载中 -->
      <!-- <div class="loading-wrap">
        <van-loading
          color="#3296fa"
          vertical
        >加载中</van-loading>
      </div> -->
      <!-- /加载中 -->
      <!-- 加载完成-文章详情 -->
      <div class="article-detail">
        <!-- 文章标题 -->
        <h1 class="article-title">{{article.title}}</h1>
        <!-- /文章标题 -->
              <van-cell class="user-info" center :border="false">
          <div slot="title" class="user-name">{{article.author}} {{article.publishDate}} 
            <van-tag  type="primary">{{article.dataSource}}</van-tag></div>
         
          <!-- <van-button
            class="follow-btn"
            round
            size="small"
          >已关注</van-button> -->
        </van-cell>
         <!-- 文章内容 -->
        <div class="article-content markdown">
         <div v-html="showtext"
             ref="article-content"
          >

          </div>
           
        </div>
        <van-divider>正文结束</van-divider>
      </div>

      

  </div>
<div class="article-bottom" v-if="optshow">
      <van-button
        class="comment-btn"
        type="info"
      
        size="small"
        :to="{name:'vuepdf',query:
        {       'tableName': this.$route.query.tableName,
                'sysId': this.$route.query.sysId,
                'fileName': this.$route.query.fileName,
                 'title':article.title
        
        }
        }"
      >在线阅读</van-button>
        <van-button
        class="comment-btn"
        type="info"
      
        size="small"
        @click="pdfdown"
      >pdf下载</van-button>
   
      
    </div>

  </div>
</template>

<script>

import './markdown.css'
import { getDocumentDetail } from '@/api/emergency'
import { ImagePreview } from 'vant';
import {pdfdown,pdfdown1} from '@/api/common' 

import { mapState } from "vuex"
export default {
  name: 'ArticleIndex',
  components: {},
  props: {
    tableName: {
      type:String,
      
    },
      sysId: {
      type:String,
     
    },
      fileName: {
      type:String,
     
    }
  },
  data () {
    return {
      article: {}, // 文章详情
      isFollowLoading:false,
      fulltext:'',
      subtext:'',
      optshow: true
    
    }
  },
  computed: {
    ...mapState(['user']),
   showtext: function () {
      if (this.$route.query.tableName != 'MYDXSPIDER_METADATA') {
    
          return this.subtext
        
      } else {
        return this.fulltext
      }
    }
  },
  watch: {},
  created () {
    this.loadArticle()
  },
  mounted () {},
  methods: {
    async loadArticle () {

         switch (this.$route.query.tableName) {
        case 'MYDXSPIDER_METADATA':
          this.optshow = false
          break

      }
      try {
        const { data } = await getDocumentDetail({
      
        tableName: this.$route.query.tableName,
        sysId: this.$route.query.sysId,
        fileName: this.$route.query.fileName
        })

       // console.log('data',data)
        this.article = data.data
        this.fulltext=data.data.text
        this.subtext=data.data.subtext
      
       //数据改变影响视图更新(DOM数据)不是立即的
       //如果需要在修改数据之后马上操作被该数据影响的视图DOM，需要把这个代码方在$nextTick中
        //得到所有的img标签
      
      this.$nextTick(()=>{
       this.handlePreviewImage()
      })
     
        //循环img列表，给img注册点击事件


        //在事件处理中调用ImagePreview

      } catch (err) {
        console.log(err)
      }
    },
    handlePreviewImage(){

        //获取文章DOM容器
       const articleContent=this.$refs['article-content']
       const imags=  articleContent.querySelectorAll('img')
       const imgPaths=[]
       imags.forEach((img,index) => {
         imgPaths.push(img.src)
         img.onclick=function(){
            ImagePreview({
            images:imgPaths ,
            startPosition: index,
         });
         }
       });

    },

    pdfdown(){
     var item={
      'tableName': this.$route.query.tableName,
      'sysId': this.$route.query.sysId,
      'fileName': this.$route.query.fileName,
     }

     pdfdown1(item)

    }

  }
  
 
}
</script>

<style scoped lang="less">
.article-container {
  .main-wrap {
    position: fixed;
    left: 0;
    right: 0;
    top: 46px;
    bottom: 44px;
    overflow-y: scroll;
    background-color: #fff;
  }
  .article-detail {
    .article-title {
      font-size: 20px;
      // padding: 50px 32px;
      padding: 14px;
      margin: 0;
      color: #3a3a3a;
    }

    .user-info {
      padding: 0 16px;
      .avatar {
        width: 35px;
        height: 35px;
        margin-right: 9px;
      }
      .van-cell__label {
        margin-top: 0;
      }
      .user-name {
        font-size: 12px;
        color: #3a3a3a;
      }
      .publish-date {
        font-size: 12px;
        color: #b7b7b7;
      }
      .follow-btn {
        width: 85px;
        height: 29px;
      }
    }

    .article-content {
      padding: 15px 10px;
      /deep/ p {
        text-align: justify;
      }
    }
  }

  .loading-wrap {
    padding: 200px 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #fff;
  }

  .error-wrap {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      font-size: 30px;
      color: #666666;
      margin: 33px 0 46px;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666666;
    }
  }

  .article-bottom {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 44px;
    border-top: 1px solid #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 141px;
      // height: 23px;
      // border: 1px solid #eeeeee;
      // font-size: 15px;
      // line-height: 23px;
      
    }
    .van-icon {
      font-size: 20px;
      .van-info {
        font-size: 11px;
        background-color: #e22829;
      }
    }
  }
}

/deep/.zhenwen_tit
{
  display:none !important;
}
/deep/.vspace{
  display: none;
}
/deep/img{
  width: 100% !important;;
}
/deep/ video{
  width: 100% !important;;
}
/deep/.MsoNormal b{
 display:none !important;
}
</style>